package com.guoguo.service.impl;

import com.github.pagehelper.PageHelper;
import com.guoguo.base.PageBean;
import com.guoguo.base.PaginationContext;
import com.guoguo.entity.AcAlbumImage;
import com.guoguo.entity.AcCategoryAlbum;
import com.guoguo.entity.Images;
import com.guoguo.entity.vo.AcCategoryAlbumVO;
import com.guoguo.entity.vo.AlbumCategoryVO;
import com.guoguo.entity.vo.CategoryAlbumVO;
import com.guoguo.enums.AliOssImageEnum;
import com.guoguo.mapper.AcAlbumImageMapper;
import com.guoguo.mapper.AcCategoryAlbumMapper;
import com.guoguo.mapper.ImagesMapper;
import com.guoguo.service.IAcCategoryAlbumService;
import com.guoguo.utils.AliOssUtils;
import com.guoguo.utils.aliOss.AliOssImageConfig;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/9 0009.
 */
@Service
public class AcCategoryAlbumServiceImpl implements IAcCategoryAlbumService {

    //AcCategoryAlbumMapper接口

    @Autowired
    private AcCategoryAlbumMapper acCategoryAlbumMapper;
    @Autowired
    private AcAlbumImageMapper acAlbumImageMapper;

    /**
     * 专辑图片列表
     *
     * @param cateId
     * @param pageNum
     * @return
     */
    public PageBean<CategoryAlbumVO> getCategoryAlbumList(String cateId, int cateType, int pageNum, int pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        List<CategoryAlbumVO> categoryAlbumVOs = null;
        if (cateType == 1) {
            categoryAlbumVOs = acCategoryAlbumMapper.getLatestAlbumList();
        }
//        else if (cateType == 2) {
//            categoryAlbumVOs = acCategoryAlbumMapper.getCategoryAlbumList(cateId);
//        }
        else {
            categoryAlbumVOs = acCategoryAlbumMapper.getCategoryAlbumList(cateId);
        }


//        AliOssImageConfig aliOssImageConfig = new AliOssImageConfig("test");

        for (CategoryAlbumVO categoryAlbumVO : categoryAlbumVOs) {
            String url = AliOssUtils.getImageUrl(categoryAlbumVO.getCoverUrl());//, aliOssImageConfig);
            String icon = AliOssUtils.getImageUrl(categoryAlbumVO.getModelIcon());//,aliOssImageConfig);
            categoryAlbumVO.setCoverUrl(url);
            categoryAlbumVO.setModelIcon(icon);
        }
        return new PageBean<CategoryAlbumVO>(categoryAlbumVOs);
    }

    @Override
    public PageBean<AlbumCategoryVO> getCategoryAlbum(String albId, int pageNum) {
        PageHelper.startPage(pageNum, PaginationContext.getPageSize());
        List<AlbumCategoryVO> categoryAlbumVO = acCategoryAlbumMapper.getCategoryAlbum(albId);
        return new PageBean<AlbumCategoryVO>(categoryAlbumVO);
    }

    @Override
    public AcCategoryAlbum findCategoryAlbum(AcCategoryAlbum acCategoryAlbum) {
        return acCategoryAlbumMapper.findCategoryAlbum(acCategoryAlbum);
    }

    @Override
    public AcCategoryAlbum findCategoryAlbumById(String cateId) {
        return acCategoryAlbumMapper.findCategoryAlbumById(cateId);
    }

    @Override
    public void updateActegoryAlbum(AcCategoryAlbum acCategoryAlbum) {
        acCategoryAlbumMapper.updateActegoryAlbum(acCategoryAlbum);

    }

    @Override
    public List<CategoryAlbumVO> selectCategoryAlbum() {
        return acCategoryAlbumMapper.selectCategoryAlbum();
    }

    @Override
    public List<AcCategoryAlbumVO> getCategoryAlbumList(String albId) {
        List<AcCategoryAlbumVO> acCategoryAlbumVOs = acCategoryAlbumMapper.getCategoryAlbums(albId);

//        AliOssImageConfig aliOssImageConfig = new AliOssImageConfig("test");

        for (AcCategoryAlbumVO acCategoryAlbumVO : acCategoryAlbumVOs) {
            Map<String, String> param = new HashedMap();
            param.put("albId", acCategoryAlbumVO.getAlbId());
            param.put("originalUrl", acCategoryAlbumVO.getCoverUrl());

            AcAlbumImage acAlbumImage = acAlbumImageMapper.getImageByIdAndUrl(param);

            acCategoryAlbumVO.setImgId(acAlbumImage.getImgId());
            acCategoryAlbumVO.setCoverUrl(AliOssUtils.getImageUrl(acCategoryAlbumVO.getCoverUrl()));//, aliOssImageConfig));
        }
        return acCategoryAlbumVOs;
    }
}
